using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using MesDashboardApi.Api.Models;
using MesDashboardApi.Api.Services;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace MesDashboardApi.Api.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class DashboardController : ControllerBase
    {
        private readonly DashboardDataService _dataService;
        private readonly IHubContext<Hubs.DashboardHub> _hubContext;

        public DashboardController(
            DashboardDataService dataService,
            IHubContext<Hubs.DashboardHub> hubContext)
        {
            _dataService = dataService;
            _hubContext = hubContext;
        }

        // 获取所有仪表盘数据
        [HttpGet]
        public ActionResult<DashboardData> GetDashboardData()
        {
            return _dataService.GetDashboardData();
        }

        // 获取生产计划数据
        [HttpGet("plan-data")]
        public ActionResult<PlanConfig> GetPlanData()
        {
            var data = _dataService.UpdatePlanData();
            return data;
        }

        // 更新生产计划数据
        [HttpPost("plan-data")]
        public async Task<ActionResult<PlanConfig>> UpdatePlanData([FromBody] List<PlanData> planData)
        {
            if (planData == null || planData.Count == 0)
            {
                return BadRequest("需要提供有效的计划数据");
            }

            try
            {
                // 更新数据
                var updatedData = _dataService.SetPlanData(planData);
                
                // 通过SignalR通知所有客户端数据已更新
                await _hubContext.Clients.All.SendAsync("ReceivePlanData", updatedData);
                
                return Ok(updatedData);
            }
            catch (Exception ex)
            {
                return StatusCode(500, $"更新生产计划数据时出错: {ex.Message}");
            }
        }

        // 获取设备运行状态数据
        [HttpGet("device-data")]
        public ActionResult<DeviceConfig> GetDeviceData()
        {
            var data = _dataService.UpdateDeviceData();
            return data;
        }

        // 获取生产概况数据
        [HttpGet("production-config")]
        public ActionResult<ProductionConfig> GetProductionConfig()
        {
            var data = _dataService.UpdateProductionConfig();
            return data;
        }

        // 获取生产线状态
        [HttpGet("production-stations")]
        public ActionResult<List<ProductionStation>> GetProductionStations()
        {
            var data = _dataService.UpdateProductionStations();
            return data;
        }

        // 获取品质监控数据
        [HttpGet("quality-data")]
        public ActionResult<QualityConfig> GetQualityData()
        {
            var data = _dataService.UpdateQualityData();
            return data;
        }

        // 获取告警信息
        [HttpGet("alarm-list")]
        public ActionResult<List<AlarmInfo>> GetAlarmList()
        {
            var data = _dataService.UpdateAlarmList();
            return data;
        }

        // 获取当前时间
        [HttpGet("current-time")]
        public ActionResult<string> GetCurrentTime()
        {
            return _dataService.GetCurrentTime();
        }

        //Test
        [HttpGet("test")]
        public ActionResult<string> Test123()
        {
            return "test";
        }
    }
} 